# -*- coding:utf-8 -*-
"""
session in redis
"""
import redis

class Session(object):
    """session class"""
    _r = dict()
    _session_id = ''

    def __init__(self, sid=''):
        """init connection redis"""
        pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=7)
        self._r = redis.Redis(connection_pool=pool)
        self._session_id = sid

    """ 
    def __setitem__(self, key, value):
        self._r.hmset(self._session_id, dict(key=value))

    def __setattr__(self, key, value):
        self._r.hmset(self._session_id, dict(key=value))

    def __getitem__(self, item):
        return self._r.hmget(self._session_id, item)

    def __getattr__(self, item):
        return self._r.hmget(self._session_id, item)
    """

    def __setitem__(self, key, value):
        """set object[key]"""
        self._r.hset(self._session_id, key, value)

    def __getitem__(self, item):
        """get"""
        if self._r.hexists(self._session_id, item):
            return self._r.hget(self._session_id, item)
        else:
            return

    def get(self, item):
        """get"""
        if self._r.hexists(self._session_id, item):
            return self._r.hget(self._session_id, item)
        else:
            return

    def set(self, key, item):
        """set"""
        self._r.hset(self._session_id, key, item)

    def set_hmset(self, items):
        self._r.hmset(self._session_id, items)

    def get_all(self):
        """get all"""
        return self._r.hgetall(self._session_id)

    def clear_all(self, items=None):
        """clear all"""
        self._r.hdel(self._session_id, items)